package exercise4;

public class Exercise10_03 {
    public static void main(String []args){
        MyInteger n = new MyInteger(33);
        System.out.println("n是偶数么 " + n.isEven());
        System.out.println("n是奇数么 " + n.isPrime());
        System.out.println("9是素数么 " + MyInteger.isPrime(9));

        char[] chars = {'2', '2', '9', '1'};
        System.out.println(MyInteger.parseInt(chars));

        String s = "1234";
        System.out.println(MyInteger.parseInt(s));

        MyInteger n2 = new MyInteger(24);
        System.out.println("n2是奇数么? " + n2.isOdd());
        System.out.println("9是奇数么？ " + MyInteger.isOdd(45));
        System.out.println("n和n2是否相等？ " + n.equals(n2));
        System.out.println("n2是否等于2? " + n2.equals(2));
    }
}

class MyInteger{
    int value;

    public MyInteger(){
    }

    public MyInteger(int new_value){
        value=new_value;
    }

    public int get(){
        return value;
    }

    public static boolean isEven(int n){
        return n%2==0;
    }

    public static boolean isOdd(int n){
        return n%2!=0;
    }

    public static boolean isPrime(int num) {
        int tmp=(int)Math.sqrt(num);
        for(int i=2;i<=tmp;i++) {
            if(0==num%i)
                return false;
        }
        return true;
    }

    public boolean isEven(){
        return isEven(value);
    }

    public boolean isOdd(){
        return isOdd(value);
    }

    public boolean isPrime(){
        return isPrime(value);
    }

    public boolean equal(int n){
        return n==value;
    }

    public boolean equal(MyInteger s){
        return s.get()==value;
    }

    public static int parseInt(char[] numbers) {
        int result = 0;
        for (int i = 0; i < numbers.length; i++) {
            result = result * 10 + (numbers[i] - '0');
        }

        return result;
    }


    public static int parseInt(String s) {
        int result = 0;
        for (int i = 0; i < s.length(); i++) {
            result = result * 10 + (s.charAt(i) - '0');
        }

        return result;
    }

}
